<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <el-input v-model="deptName" placeholder="请输入组织名称" clearable
                  size="small" prefix-icon="el-icon-search" @keyup.enter.native="handleSelectOrgCode"
        />
        <el-tree style="margin-top: 20px"
                 :data="deptOptions"
                 :filter-node-method="filterNode"
                 :expand-on-click-node="false"
                 ref="tree"
                 default-expand-all
                 @node-click="handleNodeClick"
        />
      </el-col>

      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" @submit.native.prevent>
          <el-form-item label="手机号" prop="account">
            <el-input
              v-model="queryParams.account"
              placeholder="请输入手机号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="推送内容" prop="content">
            <el-input
              v-model="queryParams.content"
              placeholder="请输入推送内容"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="状态" prop="hasRead">
            <el-select
              v-model="queryParams.hasRead"
              placeholder="状态"
              clearable
              size="small"
            >
              <el-option
                v-for="item in hasReadOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
                       v-hasPermi="['message:system:query']"
            >搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button type="warning" icon="el-icon-download" size="mini" v-hasPermi="['message:system:export']"
                       @click="handleExport"
            >导出
            </el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="messageList" border @sort-change="handleSort">
          <el-table-column label="手机号" align="center" prop="account" width="120"/>
          <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true"/>
          <el-table-column label="内容" align="center" prop="content" :show-overflow-tooltip="true"/>
          <el-table-column label="状态" align="center" prop="hasRead" width="100">
            <template slot-scope="scope">
              {{ scope.row.hasRead === '0' ? '未读' : '已读' }}
            </template>
          </el-table-column>
          <el-table-column label="组织名称" align="center" prop="deptName" :show-overflow-tooltip="true"/>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180" :show-overflow-tooltip="true"
                           sortable="custom"
          />
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { deptTreeSelect } from '@/api/system/dept'
import { exportAppMessage, listAppMessage } from '@/api/system/logManage'

export default {
  name: 'index',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 消息表格数据
      messageList: [],
      // 部门树选项
      deptOptions: undefined,
      deptName: undefined,
      // 状态下拉框
      hasReadOptions: [{ label: '未读', value: '0' }, { label: '已读', value: '1' }],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messageType: '8',
        account: undefined,
        content: undefined,
        orgCode: undefined,
        orderByCreateTime: undefined,
        hasRead: undefined,
        deptId: undefined,
        fileName: '系统消息'
      }
    }
  },
  created() {
    this.getList()
    this.getTreeselect()
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
      })
    },

    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      listAppMessage(this.queryParams).then(response => {
        this.messageList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.orgCode = data.orgCode
      this.queryParams.deptId = data.id
      this.handleQuery()
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },

    handleSelectOrgCode() {
      this.$refs.tree.filter(this.deptName)
      this.handleQuery()
    },

    /**
     * 点击排序按钮
     * @param event
     */
    handleSort(event) {
      //先清空之前的排序
      this.queryParams.orderByCreateTime = undefined

      // 转换排序的字符串, 将其转为sql中的形式
      let orderType = null
      if (event.order) {
        orderType = event.order === 'ascending' ? 'asc' : 'desc'
      }

      //先清空之前的排序
      this.queryParams.orderByCreateTime = orderType

      this.getList()
    },

    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return exportAppMessage(this.queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>

<style scoped>

</style>
